package com.example.guaiamumgigantemenu;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.guaiamumgigantemenu.R.color;

public class MyAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList<Element> data;

	public MyAdapter(Context context, ArrayList<Element> data) {
		// Caches the LayoutInflater for quicker use
		this.inflater = LayoutInflater.from(context);
		// Sets the events data
		this.data = data;
	}

	public int getCount() {
		return this.data.size();
	}

	public Element getItem(int position) throws IndexOutOfBoundsException {
		return this.data.get(position);
	}

	public long getItemId(int position) throws IndexOutOfBoundsException {
		if (position < getCount() && position >= 0) {
			return position;
		}
		return -1;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Element item = getItem(position);
		
		if (convertView == null) { // If the View is not cached
			// Inflates the Common View from XML file
			convertView = this.inflater.inflate(R.layout.item_row_layout, null);
		}
		TextView textName = (TextView) convertView.findViewById(R.id.item_name);
		textName.setText(item.getName());
		
		TextView textPrice = (TextView) convertView.findViewById(R.id.item_price);
		textPrice.setText("R$ " + item.getPrice());
		if((position % 2) == 0) {
			convertView.setBackgroundColor(Color.LTGRAY);
		} 
		return convertView;
	}
}